$(function () {
  //设备异常总数 打开窗口
  $('.unitException-trigger').on('click', function (event) {
    if (devTypeId == 1) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(1);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option><option alarmtype="3">故障</option><option alarmtype="4">低电</option>');
    } else if (devTypeId == 2) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(2);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option><option alarmtype="12">拔出</option><option alarmtype="13">过载</option><option alarmtype="14">高温</option><option alarmtype="16">长时间充电</option><option alarmtype="17">故障</option>');
    } else if (devTypeId == 3) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(3);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    } else if (devTypeId == 4) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(4);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option><option alarmtype="33">故障</option>');
    } else if (devTypeId == 5) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(5);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    } else if (devTypeId == 6) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(6);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    } else if (devTypeId == 7) {
      $('#unitExceptionModal .deviceTypeSearchBtn').val(7);
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    } else {
      $('#unitExceptionModal .deviceTypeSearchBtn').val('all');
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    }

    $("#unitExceptionModal .devAddress").val('');
    event.preventDefault();
    $('.unitException-pop').addClass('is-visible3');

    // 设备管理 首次渲染
    var unitExceptionPageBox = 1;
    $('input').val('');
    $('#unitExceptionModal .devTypeVal').val('全部');
    unitExceptionPageBoxList(unitExceptionPageBox, '', '&status=all');
    // 设备管理 获取总页数 点击分页渲染列表
    configurationSumPage(unitExceptionPageBox, '', '&status=all');
    $('.pageJump input').val('');
    $('#unitExceptionModal .devAddress').val('');
    // 渲染导出功能的列表
    // $.ajax({
    //     url:'/deviceStatus/data/list/bydevicetype?deviceTypeId=1',
    //     success: function (res) {
    //         $('#unitExceptionTableExport tbody').html('');
    //         var data = res.data;
    //         for(var i=0;i<data.length;i++){
    //             var deviceName = data[i].device.deviceName?data[i].device.deviceName:'';
    //             var usestatus = data[i].device.usestatus?data[i].device.usestatus:'';
    //             var deviceTypeName = data[i].device.deviceType.deviceTypeName?data[i].device.deviceType.deviceTypeName:'';
    //             var devCode = data[i].device.devCode?data[i].device.devCode:'';
    //             var address = data[i].device.address?data[i].device.address:'';
    //             var fixedPlace = data[i].device.fixedPlace?data[i].device.fixedPlace:'';
    //             var latitude = data[i].device.latitude?data[i].device.latitude:'';
    //             var longitude = data[i].device.longitude?data[i].device.longitude:'';
    //             var installationDate = data[i].device.installationDate;
    //             var deadline = data[i].device.deadline;

    //             var linkMain1 = data[i].device.linkMain1?data[i].device.linkMain1:'';
    //             var linkPhone1 = data[i].device.linkPhone1?data[i].device.linkPhone1:'';
    //             var linkMain2 = data[i].device.linkMain2?data[i].device.linkMain2:'';
    //             var linkPhone2 = data[i].device.linkPhone2?data[i].device.linkPhone2:'';
    //             var linkMain3 = data[i].device.linkMain3?data[i].device.linkMain3:'';
    //             var linkPhone3 = data[i].device.linkPhone3?data[i].device.linkPhone3:'';
    //             var remark = data[i].device.remark?data[i].device.remark:'';

    //             installationDate = installationDate?moment(installationDate).format("YYYY-MM-DD"):'';
    //             deadline = deadline?moment(installationDate).format("YYYY-MM-DD"):'';
    //         $('#unitExceptionTableExport tbody').append('<tr><td>'+deviceName+'</td><td>'+usestatus+'</td><td>'+deviceTypeName+'</td><td>'+devCode+'</td><td>'+address+'</td><td>'+fixedPlace+'</td><td>'+latitude+'</td><td>'+longitude+'</td><td>'+installationDate+'</td><td>'+deadline+'</td><td>'+linkMain1+'</td><td>'+linkPhone1+'</td><td>'+linkMain2+'</td><td>'+linkPhone2+'</td><td>'+linkMain3+'</td><td>'+linkPhone3+'</td><td>'+data[i].device.remark+'</td></tr>')
    //         }
    //         $('#unitExceptionModal .exportBox .export').hide();
    //         $("#unitExceptionTableExport caption").remove();
    //         $("#unitExceptionTableExport").tableExport({formats:["xlsx"]});
    //     }
    // });
  });
  $('#unitExceptionRefresh').on('click', function () {
    // 设备管理 首次渲染
    var unitExceptionPageBox = 1;
    $('input').val('');
    $('#unitExceptionModal .devTypeVal').val('全部');
    unitExceptionPageBoxList(unitExceptionPageBox, '', '&status=all');
    // 设备管理 获取总页数 点击分页渲染列表
    configurationSumPage(unitExceptionPageBox, '', '&status=all');
    $('.pageJump input').val('');
    $('#unitExceptionModal .devAddress').val('');
  });
  //设备管理  关闭窗口
  $('.unitException-pop').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close') || $(event.target).is('.unitException-pop')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
    if ($(event.target).is('.unitException-popup-close') || $(event.target).is('.unitException-pop')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
  });

  //设备管理 获取总页数 点击分页渲染列表
  function configurationSumPage(unitExceptionPageBox, devCode, status) {
    var devCode = $("#unitExceptionModal .devCodeVal").val() ? '&devCode=' + $("#unitExceptionModal .devCodeVal").val() : '';
    var devType = $("#unitExceptionModal .devTypeVal").find("option:selected").attr('alarmtype');
    var status = '&status=' + devType;


    console.log(unitExceptionPageBox, devCode, status)
    var pageSum;
    var searchContent = $("#unitExceptionModal .devAddress").val();
    if (searchContent) {
      searchContent = '&address=' + searchContent;
    } else {
      searchContent = '';
    }
    var deviceTypeId = $('#unitExceptionModal .deviceTypeSearchBtn').val();
    console.log(deviceTypeId);
    if (deviceTypeId == 'all') {
      deviceTypeId = '';
    } else {
      deviceTypeId = '&deviceTypeId=' + deviceTypeId;
    }
    $.ajax({
      url: '/paging/data/list/bydevicetype/pageNumber?' + status + deviceTypeId + devCode + searchContent,
      async: false,
      success: function (result) {
        pageSum = result.data;
        unitExceptionPageBoxFunc(pageSum, unitExceptionPageBox, devCode, status)
      }
    });
  }
  //设备管理  分页功能
  function unitExceptionPageBoxFunc(pageSum, unitExceptionPageBox, devCode, status) {
    if (pageSum <= 1) {
      $('#unitExceptionPageBox .pageJump').hide()
    } else {
      $('#unitExceptionPageBox .pageJump').show()
    }
    Page({
      num: pageSum, //页码数
      startnum: unitExceptionPageBox, //指定页码
      elem: $('#unitExceptionPage'), //指定的元素
      callback: function (n) { //回调函数
        unitExceptionPageBoxList(n, devCode, status);
      }
    });
  };
  //设备异常  页面渲染列表
  function unitExceptionPageBoxList(n, devCode, status) {
    var devCode = $("#unitExceptionModal .devCodeVal").val() ? '&devCode=' + $("#unitExceptionModal .devCodeVal").val() : '';
    var devType = $("#unitExceptionModal .devTypeVal").find("option:selected").attr('alarmtype');
    var status = 'status=' + devType;
    var searchContent = $("#unitExceptionModal .devAddress").val();
    if (searchContent) {
      searchContent = '&address=' + searchContent;
    } else {
      searchContent = '';
    }
    var deviceTypeId = $('#unitExceptionModal .deviceTypeSearchBtn').val();
    console.log(deviceTypeId);
    if (deviceTypeId == 'all') {
      deviceTypeId = '';
    } else {
      deviceTypeId = '&deviceTypeId=' + deviceTypeId;
    }
    $.ajax({
      url: '/paging/data/list/bydevicetype/approve?' + status + deviceTypeId + searchContent + devCode + '&page=' + n,
      async: false,
      success: function (result) {
        console.log(result)
        $('#unitExceptionModal .tbodyList').html('');
        unitExceptionPageBox = n;
        var data = result.data;
        if (data.length != 0) {
          for (var i = 0; i < data.length; i++) {
            var beginTime = data[i].returnTime;
            beginTime = moment(beginTime).format("YYYY-MM-DD HH:mm:ss");
            var alarmId = data[i].alarmId;
            var status = data[i].status;
            if (status == 1) {
              status = '';
            } else if (status == 3) {
              status = '故障';
            } else if (status == 4) {
              status = '低电';
            } else if (status == 7) {
              status = '';
            }
            var fixedPlace = data[i].device.fixedPlace ? data[i].device.fixedPlace : '';
            //                  <span class="configBtnStyle modifyBtn editDevice-trigger">修改</span>
            $('#unitExceptionModal .tbodyList').append('<tr><td>' + ((+n - 1) * (+8) + (+i) + (+1)) + '</td><td>' + data[i].devCode + '</td><td>' + data[i].device.address + '</td><td>' + fixedPlace + '</td><td>' + status + '</td><td>' + beginTime + '</td><td>' + data[i].device.fireCount + '</td></tr>')
          }
        } else {
          unitExceptionPageBox = unitExceptionPageBox - 1;
          // 设备管理 首次渲染
          unitExceptionPageBoxList(unitExceptionPageBox, devCode, status);
          // 设备管理 获取总页数 点击分页渲染列表
          configurationSumPage(unitExceptionPageBox, devCode, status);
          $('.pageJump input').val('');
        }
      }
    });
  };
  //全选删除/全不选
  $('#unitExceptionSelectAll').click(function () {
    var current = $(this).prop('checked');
    $('.check').prop({
      checked: current
    });
  });

  $('#unitExceptionModal').on('click', '.check', function () {
    var flag = true;
    //  遍历下面所有的状态
    for (var i = 0; i < $('.check').length; i++) {
      //        如果其中一个不满足条件，把状态设置成false，立刻跳出循环
      if ($('.check').eq(i).prop('checked') == false) {
        flag = false;
        break;
      }
    }
    $('#unitExceptionSelectAll').prop({
      checked: flag
    });
  });

  //设备管理 设备删除
  $("#unitExceptionDelBtn").on('click', function () {
    $('#unitExceptionModal .table .tbodyList input[type=checkbox]:checked').each(function () {
      var devid = $(this).attr('devid');
      $.ajax({
        url: '/device/del/' + devid,
        async: false,
        success: function (res) {
          console.log(res)
        }
      });
    });
    var devCode = $("#unitExceptionModal .devCodeVal").val() ? '&devCode=' + $("#unitExceptionModal .devCodeVal").val() : '';
    var devType = $("#unitExceptionModal .devTypeVal").find("option:selected").attr('alarmtype');
    var status = '&status=' + devType;
    // 设备管理 删除后重新渲染
    unitExceptionPageBoxList(unitExceptionPageBox, devCode, status);
    // 设备管理 获取总页数 点击分页渲染列表
    configurationSumPage(unitExceptionPageBox, devCode, status);
    $('.pageJump input').val('');
  });
  //设备管理 修改
  $('#unitExceptionModal .manageModal .tableModal').on('click', '#deviceModifyBtn', function () {
    var $thisParent = $(this).parent();
    var devCode = $thisParent.attr('devcode');
    var alarmId = $thisParent.attr('alarmid');
    var disposeStatus = 1;
    configurationHandle(devCode, alarmId, disposeStatus);

    // 设备管理 首次渲染
    unitExceptionPageBoxList(unitExceptionPageBox, devCode, status);
    // 设备管理 获取总页数 点击分页渲染列表
    configurationSumPage(unitExceptionPageBox, devCode, status);
    $('.pageJump input').val('');

    //    弹出修改页面模态框

  });

  //设备管理 点击定位定位到地图
  //$('#unitExceptionModal .tableModal .tbodyList').on('click','#devicePositionBtn',function (){
  //  var $thisParent = $(this).parent();
  //  var longitude = $thisParent.attr('longitude');
  //  var latitude = $thisParent.attr('latitude');
  //  var devCode = $thisParent.attr('devCode');
  //  BaiDuMap.panTo(latitude,longitude);
  //  setTimeout(function(){
  //  BaiDuMap.popMenu(devCode);
  //  },500);
  ////  BaiDuMap.deletePoint
  ////  BaiDuMap.addMarker(fireCount, status, devCode, deviceName, address,latitude,longitude, linkMain1, linkPhone1, linkMain2, linkPhone2, linkMain3, linkPhone3, linkMain4, linkPhone4)
  //  
  //  if ($(event.target).is('.editDevice-popup-close') || $(event.target).is('.editDevice-pop')) {
  //      event.preventDefault();
  //      $(this).removeClass('is-visible3');
  //  }
  //  
  //});
  // 模糊查询
  searchFunc();

  function searchFunc() {


    //模糊查询
    $('body').on('click', function () {
      $("#unitExceptionModal .searchList").hide();
    });
    var fn = _.debounce(function () {
      $("#unitExceptionModal .searchList").show();
      hide2border();
      if ($("#unitExceptionModal .searchList li")) { //键盘抬起后就判断页面中是否存在这个元素,存在就删除
        $("#unitExceptionModal .searchList li").remove();
      }
      var searchContent = $("#unitExceptionModal .devAddress").val();
      //查询内容存在则搜索
      if (searchContent) {
        searchContent = '&address=' + searchContent;
        var deviceTypeId = $('#unitExceptionModal .deviceTypeSearchBtn').val();
        console.log(deviceTypeId);
        if (deviceTypeId == 'all') {
          deviceTypeId = '';
        } else {
          deviceTypeId = '&deviceTypeId=' + deviceTypeId;
        }
        $.ajax({
          url: "/device/data/list/bydeviceTypeandaddresslike?" + searchContent + deviceTypeId,
          type: "GET",
          async: false,
          success: function (result) {

            if (result.status == "200") {
              for (var i = 0; i < result.data.length; i++) {
                var searchAddress = result.data[i].address;
                var latitude = result.data[i].latitude;
                var longitude = result.data[i].longitude;
                var devCode = result.data[i].devCode;
                if (searchAddress) {
                  $("#unitExceptionModal .searchList").append('<li devicecode="' + devCode + '" longitude="' + longitude + '" latitude="' + latitude + '"><i class="fa fa-map-marker"></i>' + searchAddress + '</li>');
                  hide2border();
                }
              }
            } else {

            }
          }
        });

      } else {
        //          去掉定位按钮的属性
        //            $(".searchBox #search-btn").removeAttr("devicecode");
        //            $(".searchBox #search-btn").removeAttr("longitude");
        //            $(".searchBox #search-btn").removeAttr("latitude");

      }
      $("#unitExceptionModal .searchList").show();
      hide2border();
    }, 500);
    $("#unitExceptionModal .devAddress").on("input", function (e) {
      fn();
    });
    //直接输入地址,点击列表查询
    $("#unitExceptionModal .searchList").on("click", "li", function () {
      $("#unitExceptionModal .searchList").show();
      var liVal = $(this).text();
      var searchVal = $("#unitExceptionModal .devAddress").val(liVal);

      $("#unitExceptionModal .searchList").html(""); //hide
      var li = $(this);
      var devicecode = li.attr("devicecode");
      var latitude = li.attr("latitude");
      var longitude = li.attr("longitude");
      //        map.setZoom(20);
      //        BaiDuMap.panTo(latitude, longitude);
      ////        地图信息框显示
      //        BaiDuMap.popMenu(devicecode);
      return false;
    });
  }

  function hide2border() {
    if (!($("#unitExceptionModal .searchList>li").length)) {
      $("#unitExceptionModal .searchList").hide();
    } else {
      $("#unitExceptionModal .searchList").show();
    }
  }
  //点击设备管理搜索按钮 unitExceptionSearchBtn
  var unitExceptionClickSearchFn = _.debounce(function () {
    var devCode = $("#unitExceptionModal .devCodeVal").val() ? '&devCode=' + $("#unitExceptionModal .devCodeVal").val() : '';
    var devType = $("#unitExceptionModal .devTypeVal").find("option:selected").attr('alarmtype');
    var status = '&status=' + devType;

    console.log(unitExceptionPageBox, devCode, status);
    configurationSumPage(unitExceptionPageBox, devCode, status);
    unitExceptionPageBoxList(1, devCode, status);
    $('.pageJump input').val('');
  }, 500);
  $("#unitExceptionSearchBtn").on('click', function () {
    unitExceptionClickSearchFn();

  });
  // 根据设备类型添加设备状态
  
  $('#unitExceptionModal .deviceTypeSearchBtn').change(function () {
    var unitExceptionDevTypeId = $(this).val();
    console.log(unitExceptionDevTypeId)
    unitExceptionDevStatus(unitExceptionDevTypeId);
  });
  function unitExceptionDevStatus(unitExceptionDevTypeId) {
    if (unitExceptionDevTypeId == 'all') {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    }else if (unitExceptionDevTypeId == 1) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option><option alarmtype="3">故障</option><option alarmtype="4">低电</option>');
    }else if (unitExceptionDevTypeId == 2) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option><option alarmtype="12">拔出</option><option alarmtype="13">过载</option><option alarmtype="14">高温</option><option alarmtype="16">长时间充电</option><option alarmtype="17">故障</option>');
    }else if (unitExceptionDevTypeId == 3) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    }else if (unitExceptionDevTypeId == 4) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option><option alarmtype="33">故障</option>');
    }else if (unitExceptionDevTypeId == 5) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    }else if (unitExceptionDevTypeId == 6) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    }else if (unitExceptionDevTypeId == 7) {
      $('#unitExceptionModal .devTypeVal').html('<option alarmtype="all" class="select">全部</option>');
    }
  }
});